package com.uls.web.controller.zsw;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.zsw.domain.MeterInfo;
import com.uls.zsw.service.IMeterInfoService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 电表信息Controller
 * 
 * @author zsw
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/zsw/meter")
public class MeterInfoController extends BaseController
{
    @Autowired
    private IMeterInfoService meterInfoService;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private MinioProp minioProp;

    /**
     * 查询电表信息列表
     */
    @PreAuthorize("@ss.hasPermi('zsw:meter:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeterInfo meterInfo)
    {
        startPage();
        List<MeterInfo> list = meterInfoService.selectMeterInfoList(meterInfo);
        return getDataTable(list);
    }

    /**
     * 导出电表信息列表
     */
    @PreAuthorize("@ss.hasPermi('zsw:meter:export')")
    @Log(title = "电表信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeterInfo meterInfo)
    {
        List<MeterInfo> list = meterInfoService.selectMeterInfoList(meterInfo);
        ExcelUtil<MeterInfo> util = new ExcelUtil<MeterInfo>(MeterInfo.class);
        util.exportExcel(response, list, "电表信息数据");
    }

    /**
     * 获取电表信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('zsw:meter:query')")
    @GetMapping(value = "/{meterId}")
    public AjaxResult getInfo(@PathVariable("meterId") Long meterId)
    {
        return success(meterInfoService.selectMeterInfoByMeterId(meterId));
    }

    /**
     * 新增电表信息
     */
    @PreAuthorize("@ss.hasPermi('zsw:meter:add')")
    @Log(title = "电表信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeterInfo meterInfo)
    {
        return toAjax(meterInfoService.insertMeterInfo(meterInfo));
    }

    /**
     * 修改电表信息
     */
    @PreAuthorize("@ss.hasPermi('zsw:meter:edit')")
    @Log(title = "电表信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeterInfo meterInfo)
    {
        return toAjax(meterInfoService.updateMeterInfo(meterInfo));
    }

    /**
     * 删除电表信息
     */
    @PreAuthorize("@ss.hasPermi('zsw:meter:remove')")
    @Log(title = "电表信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{meterIds}")
    public AjaxResult remove(@PathVariable Long[] meterIds)
    {
        return toAjax(meterInfoService.deleteMeterInfoByMeterIds(meterIds));
    }

    @PreAuthorize("@ss.hasPermi('zsw:meter:mmodellist')")
    @GetMapping("/mmlist")
    public AjaxResult getmmlist(){
        return success(meterInfoService.selectAllMeterModel());
    }

    /**
     * 上传头像图片
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/uploadphoto")
    public String uploadphoto(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }
}
